home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-31 | 4.3 KB | 217 lines | [TEXT/MMCC] |
- /********************************************
- **** Core Class Demo by Yves Schmid
- ****
- **** Connections.cp
- ****
- **** Author: Yves Schmid
- **** Created: 14 August 1994
- **** Modified: 14 August 1994
- **** Version: 1.0
- **** Compatible: C++
- ****
- **** Description: Connections and duplication demo
- ****
- *************************/
-
-
- #include "CoreHead.h"
-
- #include <stdio.h>
- #include <string.h>
-
- //*************************************
-
-
- const long cmd_printmyself = 1024; // A command which asks the stringnode to print its string
-
-
- //*************************************
- // A simple CoreHead which stocks and prints a string
-
- class stringnode : public CoreHead
- {
- char data[256];
-
-
- protected:
-
- // Override the "receivecmd" to handle the "cmd_printmyself" command:
- void receivecmd(long cmd, void *info);
-
- public:
-
- inline char *getstring(void) {return data;}
-
- // Constructors
-
- stringnode(char *str, CoreHead *supervisor); // Object with a supervisor
- stringnode(char *str =NULL); // Unlinked object
-
- // Destructor
-
- ~stringnode();
-
- // Duplication system:
- Core *duplicate(void);
- void copyfrom(Core *);
- };
-
- //..............................
-
- stringnode::stringnode(char *str, CoreHead *supervisor) : CoreHead(supervisor,0,1)
- // Initialize CoreHead with the supervisor,
- // destination entry 0 and 1 entry in the new
- // created object
-
- {
- newlevel(); // A new heritage level!
- strcpy(data,str); // Copy string
- }
-
- //..............................
-
-
- stringnode::stringnode(char *str) : CoreHead(1) // Unlinked object with one entry
- {
- newlevel(); // A new heritage level!
-
- if (str) strcpy(data,str); // Copy string
- else str[0] = 0; // else empty string
- }
-
- //..............................
-
- stringnode::~stringnode()
- {
- printf("%s deleted\n", data);
- }
-
- //..............................
-
-
- void stringnode::receivecmd(long cmd, void *info)
- {
- switch(cmd)
- {
- case cmd_printmyself: // Ok it is the right message, I can print the string.
- printf("%s\n",data);
- break;
-
- default: // Unknow message? Call the parent class
- CoreHead::receivecmd(cmd,info);
- }
- }
-
- //..............................
-
- Core *stringnode::duplicate(void)
- {
- stringnode *no;
-
- clearerror();
-
- no = new stringnode();
-
- if (no)
- {
- no->copyfrom(this);
- importerror(no);
- }
- else adderror(CERR_NOMEMORY);
-
- return no;
- }
-
- void stringnode::copyfrom(Core *core)
- {
-
- CoreHead::copyfrom(core);
-
- if (core->getlevel()>=getlevel())
- {
- strcpy(data,((stringnode*)core)->getstring());
- strcat(data,"-2");
- }
- }
-
- //..............................
-
-
- //*******************************************************************
-
-
- void main()
- {
- // Creating a tree:
- //
- // A
- // / \
- // B1 B2
- // / \ / \
- // C1 C2 D1 D2
-
- printf("*** CCL Tree demo ***\n\n");
- printf("Creating a tree:\n\n");
- printf(" A \n");
- printf(" / \\ \n");
- printf(" B1 B2 \n");
- printf(" /\\ /\\ \n");
- printf(" C1 C2 D1 D2 \n");
-
- //*****************************
-
- stringnode *a,*b1,*b2,*c1,*c2,*d1,*d2,*tree2;
-
- a = new stringnode("A");
- b1 = new stringnode("B1",a);
- b2 = new stringnode("B2",a);
- c1 = new stringnode("C1",b1);
- c2 = new stringnode("C2",b1);
- d1 = new stringnode("D1",b2);
- d2 = new stringnode("D2",b2);
-
- //*****************************
-
- printf("\n\n* Duplicate \"B1\", result is a new tree:\n");
- printf(" B1-2 \n");
- printf(" / \\ \n");
- printf(" C1-2 C2-2 \n");
-
- tree2 = (stringnode*)b1->duplicate();
- tree2->remove(); // Removes new tree from its supervisor which is "A"
-
- printf("\n\n* Scan the new tree:\n");
- tree2->docmd(cmd_printmyself,CCF_EVERYWHERE);
-
-
- //*****************************
-
- printf("\n\n* Connect the root of the second tree to \"B2\", new structure is:\n");
- printf(" A \n");
- printf(" / \\ \n");
- printf(" B1 B2<----------B1-2 \n");
- printf(" /\\ /\\ / \\ \n");
- printf(" C1 C2 D1 D2 C1-2 C2-2 \n");
-
-
- tree2->connectto(b2);
-
- printf("\n\n* Scan the full structure using the root of the second tree (B1-2):\n");
- tree2->docmd(cmd_printmyself,CCF_EVERYWHERE);
-
-
- //*****************************
-
-
- printf("\n\n* Delete the \"B1\" node:\n");
- delete b1;
-
- printf("\n\n* Delete the \"A\" node:\n");
- delete a;
-
- printf("\n\n* Delete the second tree:\n");
- delete tree2;
- }
-
-
-